﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BanDTBLL;
using PagedList;
namespace _1061102_1061114_1061129_1061132_1061141.Controllers
{ 
    public class TaiKhoanController : Controller
    {
        private ban_dien_thoai_dbEntities db = new ban_dien_thoai_dbEntities();
        
        //
        // GET: /TaiKhoan/

        public ViewResult ThanhCong()
        {
            return View();
        }

        public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "NgaySinhdesc" : "";
            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;
            var taikhoan = from s in db.tai_khoan
                           select s;
            if (!String.IsNullOrEmpty(searchString))
            {
                taikhoan = taikhoan.Where(s => s.ten_dang_nhap.ToUpper().Contains(searchString.ToUpper())
                                       || s.ho_ten.ToUpper().Contains(searchString.ToUpper()));
            }
            switch (sortOrder)
            {
                case "NgaySinhdesc":
                    taikhoan = taikhoan.OrderByDescending(s => s.ten_dang_nhap);
                    break;
                default:
                    taikhoan = taikhoan.OrderBy(s => s.ten_dang_nhap);
                    break;
            }
            var tai_khoan = db.tai_khoan.Include("loai_tai_khoan");
            int pageSize = 3;
            int pageNumber = (page ?? 1);
            return View(taikhoan.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /TaiKhoan/Details/5

        public ViewResult Details(int id)
        {
            tai_khoan tai_khoan = db.tai_khoan.Single(t => t.ma == id);
            return View(tai_khoan);
        }

        //
        // GET: /TaiKhoan/Create

        public ActionResult Create()
        {
            ViewBag.ma_loai_tai_khoan = new SelectList(db.loai_tai_khoan, "ma", "ten");
            return View();
        } 

        //
        // POST: /TaiKhoan/Create

        [HttpPost]
        public ActionResult Create(tai_khoan tai_khoan)
        {
            if (ModelState.IsValid)
            {
                if (tai_khoan.ma_loai_tai_khoan == null)
                {
                    tai_khoan.ma_loai_tai_khoan = 2;
                }
                db.tai_khoan.AddObject(tai_khoan);
                db.SaveChanges();
                return RedirectToAction("ThanhCong");
            }

            ViewBag.ma_loai_tai_khoan = new SelectList(db.loai_tai_khoan, "ma", "ten", tai_khoan.ma_loai_tai_khoan);
            return View(tai_khoan);
        }
        
        //
        // GET: /TaiKhoan/Edit/5
 
        public ActionResult Edit(int id)
        {
            tai_khoan tai_khoan = db.tai_khoan.Single(t => t.ma == id);
            ViewBag.ma_loai_tai_khoan = new SelectList(db.loai_tai_khoan, "ma", "ten", tai_khoan.ma_loai_tai_khoan);
            return View(tai_khoan);
        }

        //
        // POST: /TaiKhoan/Edit/5

        [HttpPost]
        public ActionResult Edit(tai_khoan tai_khoan)
        {
            if (tai_khoan.ma_loai_tai_khoan == null)
            {
                tai_khoan.ma_loai_tai_khoan = 2;
            }
            db.tai_khoan.Attach(tai_khoan);
            db.ObjectStateManager.ChangeObjectState(tai_khoan, EntityState.Modified);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // GET: /TaiKhoan/Delete/5
 
        public ActionResult Delete(int id)
        {
            tai_khoan tai_khoan = db.tai_khoan.Single(t => t.ma == id);
            return View(tai_khoan);
        }

        //
        // POST: /TaiKhoan/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            tai_khoan tai_khoan = db.tai_khoan.Single(t => t.ma == id);
            db.tai_khoan.DeleteObject(tai_khoan);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}